package com.cw.demo.bootstrap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import java.util.Locale;

@Service
public class MessageSourceHandler {

    @Autowired
    private MessageSource messageSource;

    //国际化转换
    public String locale(String messageKey) {
        Locale locale = LocaleContextHolder.getLocale();
        if (locale.getLanguage() == "en") {
            locale = new Locale("en", "US");
        } else if (locale.getLanguage() == "zh") {
            locale = new Locale("zh", "CN");
        } else if (locale.getLanguage() == "ja") {
            locale = new Locale("ja", "JP");
        } else if (locale.getLanguage() == "kr") {
            locale = new Locale("ko", "KR");
        } else if (locale.getLanguage() == "tc") {
            locale = new Locale("zh", "TW");
        }
        //test是配置文件中的key
        String msg = messageSource.getMessage(messageKey, null, locale);
        System.out.println(msg);
        return msg;
    }
}